package com.hzp.lolnews.ui.discovery.model;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzp.lolnews.bean.DiscoveryContent;
import com.hzp.lolnews.bean.DiscoveryNav;
import com.hzp.lolnews.utils.OkHttpClientUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/10/21 0021.
 */
public class IDiscoveryImpl implements IDiscovery {


    @Override
    public void loadDiscovery(final String[] url, final OnLoadDiscoveryListener listener) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    //得到json字符串
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url[0]);
                    //传递数据
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

            @Override
            public void call(String s) {
                try {
                    //接收数据并解析
                    JSONObject object = new JSONObject(s);
                    String rows = object.getString("data");
                    Gson gson = new Gson();
                    List<DiscoveryNav> discoveryNavs = gson.fromJson(rows, new TypeToken<List<DiscoveryNav>>() {
                    }.getType());
                    listener.onSuccessDiscoveryNav(discoveryNavs);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });

        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                try {
                    //得到json字符串
                    String stringFromUrl = OkHttpClientUtils.getStringFromUrl(url[1]);
                    //传递数据
                    subscriber.onNext(stringFromUrl);
                    subscriber.onCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

            }
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

            @Override
            public void call(String s) {
                Log.i("TAG", "call:--------------------------------------- " + s);
                try {
                    //接收数据并解析
                    JSONObject object = new JSONObject(s);
                    String rows = object.getString("data");
                    Log.i("TAG", "call:=============================== data" + rows);
                    Gson gson = new Gson();
                    List<DiscoveryContent> discoveryContents = gson.fromJson(rows, new TypeToken<List<DiscoveryContent>>() {
                    }.getType());
                    listener.onSuccessDiscoveryContent(discoveryContents);
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onFailure("数据异常", e);
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                listener.onFailure("网络异常", throwable);
            }
        });
    }


}
